/* Program : TestWSPBuilder
 * Created by: Carsten Keutmann
 * Date : 2007
 *  
 * The WSPBuilder comes under GNU GENERAL PUBLIC LICENSE (GPL).
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace TestWSPBuilder.Library
{
    public class TestInvironment
    {
        private static readonly TestInvironment instance = new TestInvironment();

        private string projectsPath
        {
          get 
          { 
             DirectoryInfo dir = new DirectoryInfo(Environment.CurrentDirectory+@"\..\..\projects");
             return dir.FullName; 
          }
        }



        public static string ProjectsPath
        {
            get
            {
                return Instance.projectsPath;
            }
        }



        public static void CleanUp(string projectName)
        {
            string currentProjectPath = Instance.projectsPath + @"\" + projectName ;
            if (File.Exists(currentProjectPath + @"\manifest.xml"))
            {
                File.Delete(currentProjectPath + @"\manifest.xml");
            }

        }


        public static void CompareManifests(string projectName)
        {
            string currentProjectPath = Instance.projectsPath + @"\" + projectName;

            if (!Instance.FileCompare(currentProjectPath + @"\manifest.xml", currentProjectPath + @"\Control_manifest.xml"))
            {
                throw new ApplicationException("Manifest.xml file in project " + projectName + " do not match the control_manifest.xml file!");
            }
        }


        // This method accepts two strings the represent two files to 
        // compare. A return value of 0 indicates that the contents of the files
        // are the same. A return value of any other value indicates that the 
        // files are not the same.
        private bool FileCompare(string file1, string file2)
        {
            int file1byte;
            int file2byte;
            FileStream fs1;
            FileStream fs2;

            // Determine if the same file was referenced two times.
            if (file1 == file2)
            {
                // Return true to indicate that the files are the same.
                return true;
            }

            // Open the two files.
            fs1 = new FileStream(file1, FileMode.Open);
            fs2 = new FileStream(file2, FileMode.Open);

            // Check the file sizes. If they are not the same, the files 
            // are not the same.
            if (fs1.Length != fs2.Length)
            {
                // Close the file
                fs1.Close();
                fs2.Close();

                // Return false to indicate files are different
                return false;
            }

            // Read and compare a byte from each file until either a
            // non-matching set of bytes is found or until the end of
            // file1 is reached.
            // Do not compare the first 120 bytes
            int i = 0;
            do
            {
                i++;
                // Read one byte from each file.
                file1byte = fs1.ReadByte();
                file2byte = fs2.ReadByte();

                if (i > 120)
                {
                    if (file1byte != file2byte)
                    {
                        break;
                    }
                }
            }
            while ((file1byte != -1));

            // Close the files.
            fs1.Close();
            fs2.Close();

            // Return the success of the comparison. "file1byte" is 
            // equal to "file2byte" at this point only if the files are 
            // the same.
            return ((file1byte - file2byte) == 0);
        }
	

        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static TestInvironment()
        {
        }

        TestInvironment()
        {
        }

        public static TestInvironment Instance
        {
            get
            {
                return instance;
            }
        }


    }
}
